﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using ZK.Qymd_TransManage.Model;

namespace ZK.Qymd_TransManage
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load("UserData.xml");
            int i = dgViewLogin.CurrentRow.Index;
            if (dgViewLogin.Rows[i].Selected == true)
            {
                string id = dgViewLogin.Rows[i].Cells[0].Value.ToString();
                //MessageBox.Show(id);
                XmlNode node = document.SelectSingleNode($"/users/user[@id = {id}]");
                if (node != null)
                {
                    document.DocumentElement.RemoveChild(node);
                    document.Save("UserData.xml");
                    LoadUsers();
                    MessageBox.Show("删除成功");
                }
                else {
                    MessageBox.Show("未能找到选中项");
                }
            }
        }
        private void Register_Load(object sender, EventArgs e)
        {
            LoadUsers();
        }
        public void LoadUsers() {
            //DataTable dt = (DataTable)dgViewLogin.DataSource;
            //dt.Rows.Clear();
            //dgViewLogin.DataSource = dt;
            XmlDocument document = new XmlDocument();
            document.Load("UserData.xml");
            XmlNodeList nodeList = document.SelectNodes("/users/user");
            List<DataGridModel> ListDataModel = new List<DataGridModel>();
            ListDataModel.Clear();
            DataGridModel dataModel = null;
            foreach (XmlNode userNode in nodeList)
            {
                dataModel = new DataGridModel();
                // MessageBox.Show(userNode.Attributes["name"].Value.ToString());
                dataModel.id = userNode.Attributes["id"].Value.ToString();
                dataModel.name = userNode.SelectSingleNode("name").InnerText;
                //dataModel.username = userNode.SelectSingleNode("username").InnerText;
                dataModel.password = userNode.SelectSingleNode("password").InnerText;
                ListDataModel.Add(dataModel);
            }
            dgViewLogin.AutoGenerateColumns = false;
            dgViewLogin.DataSource = ListDataModel;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddAdmin add = new AddAdmin();
            add.ShowDialog();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadUsers();
        }
    }
}
